package com.cn.lxf.sharding_test.service.impl;

import com.cn.lxf.sharding_test.entity.User;
import com.cn.lxf.sharding_test.mapper.UserRepository;
import com.cn.lxf.sharding_test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserRepository userRepository;

	@Override
	public void processUsers() throws SQLException {
		insertUsers();
	}

	private List<Long> insertUsers() throws SQLException {
		List<Long> result = new ArrayList<>(10);
		for (Long i = 1L; i <= 10; i++) {
			User user = new User();
			user.setUserId(i);
			user.setUserName("user_" + i);
			userRepository.addUser(user);
			result.add(user.getUserId());
			System.out.println("Insert User:" + user.getUserId());
	        
		}
		return result;
	}

	@Override
	public List<User> getUsers() throws SQLException {
		Long start = System.currentTimeMillis();
		List<User> list =  userRepository.findusers();
		Long end = System.currentTimeMillis();
		System.out.println("查询结果 耗时" + (end - start) /1000 + "秒");
		 return list;
	}
}
